package com.duobaoyu.dby.supply.biz.producer.deflistener.impl;

import com.duobaoyu.dby.supply.core.cache.service.LocalCacheService;
import com.duobaoyu.dby.supply.core.dinger.annotation.DingerTalk;
import com.duobaoyu.dby.supply.core.rocketmq.constant.CommonTopicConstant;
import com.duobaoyu.dby.supply.core.rocketmq.handler.AbstractBroadcastMessageHandler;
import com.duobaoyu.dby.supply.core.rocketmq.message.ResourceLocalCacheMessage;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @Author tianxiaoqiang
 * @Date 2022/8/2 10:36
 * @Version 1.0
 * @Description 状态映射广播消息
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class LocalCacheUpdateMessageHandler extends AbstractBroadcastMessageHandler<ResourceLocalCacheMessage> {

	private final LocalCacheService cacheService;
	@Value("${spring.application.name}")
	private String name;

	@Override
	public String getTopic() {
		return CommonTopicConstant.LocalCacheBroadcast.TOPIC;
	}

	@Override
	public String getTag() {
		return CommonTopicConstant.LocalCacheBroadcast.Tag.RESOURCE_LOCAL_CACHE_CHANGE_TAG;
	}

	@Override
	@DingerTalk(title = "${" + "spring.application.name" + ":" + "supply-chain-default" + "}" + "接收到状态映射缓存变更消息Consumer")
	public boolean messageHandler(ResourceLocalCacheMessage body, MessageExt messageExt) {
		if(log.isDebugEnabled()) {
			log.debug("供应链{}接收到状态映射缓存变更消息:stateMapping = {}", this.name, body);
		}
		cacheService.removeAllCache();
		if(log.isDebugEnabled()) {
			log.debug("供应链{}接收到状态映射缓存变更消息:messageExt = {}", this.name, messageExt.toString());
		}
		return Boolean.TRUE;
	}
}
